#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# author: Floydawong


from PIL import Image
import os
import json


CONFIG_JSON_PATH = 'images.json'
OUTPUT_DIR = './output'
SUFFIX = 'png'


def parse_json():
    with open(CONFIG_JSON_PATH, 'rb') as f:
        content = f.read()
        return json.loads(content)


os.makedirs(OUTPUT_DIR, exist_ok=True)


if __name__ == '__main__':
    for config in parse_json():
        name = config.get('name')
        size = config.get('size')
        color = config.get('color')

        # _Mode = Literal["1", "CMYK", "F", "HSV", "I", "L", "LAB", "P", "RGB", "RGBA", "RGBX", "YCbCr"]
        # def new(mode: _Mode, size: tuple[int, int], color: float | tuple[float, ...] | str = ...) -> Image: ...
        mode = 'RGBA'
        size = tuple(size)

        img = None
        if type(color) == list:
            img = Image.new(mode, size, tuple(color))
        if type(color) == str:
            img = Image.new(mode, size, color)

        file_name = name + '.' + SUFFIX
        img.save(os.path.join(OUTPUT_DIR, file_name))
        print('{} saved...'.format(file_name))
